package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Arrays;

public class Main3Activity extends AppCompatActivity implements View.OnClickListener{
    Button btn_0;
    Button btn_1;
    Button btn_2;
    Button btn_3;
    Button btn_4;
    Button btn_5;
    Button btn_6;
    Button btn_7;
    Button btn_8;
    Button btn_9;
    Button btn_dian;
    Button btn_dy;
    Button btn_clear;
    Button btn_del;
    Button btn_jia;
    Button btn_jian;
    Button btn_chen;
    Button btn_chu;
    EditText et_result;
    private StringBuilder pending=new StringBuilder();

    private void initView() {
        btn_0 = (Button) findViewById(R.id.btn_0);
        btn_1 = (Button) findViewById(R.id.btn_1);
        btn_2 = (Button) findViewById(R.id.btn_2);
        btn_3 = (Button) findViewById(R.id.btn_3);
        btn_4 = (Button) findViewById(R.id.btn_4);
        btn_5 = (Button) findViewById(R.id.btn_5);
        btn_6 = (Button) findViewById(R.id.btn_6);
        btn_7 = (Button) findViewById(R.id.btn_7);
        btn_8 = (Button) findViewById(R.id.btn_8);
        btn_9 = (Button) findViewById(R.id.btn_9);
        btn_jia = (Button) findViewById(R.id.btn_jia);
        btn_jian = (Button) findViewById(R.id.btn_jian);
        btn_chen = (Button) findViewById(R.id.btn_chen);
        btn_chu = (Button) findViewById(R.id.btn_chu);
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_del = (Button) findViewById(R.id.btn_del);
        et_result=(EditText)findViewById(R.id.et_result);
        btn_dy=(Button)findViewById(R.id.btn_dy) ;
        btn_dian=(Button)findViewById(R.id.btn_dian) ;

        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_jia.setOnClickListener(this);
        btn_jian.setOnClickListener(this);
        btn_chen.setOnClickListener(this);
        btn_chu.setOnClickListener(this);
        btn_dian.setOnClickListener(this);
        btn_dy.setOnClickListener(this);
        et_result.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_del.setOnClickListener(this);

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
        public void onClick(View v){
            int last = 0;
            if(pending.length()!=0)
            {
                last = pending.codePointAt(pending.length()-1);

            }
            switch (v.getId()) {
                case R.id.btn_0:
                    pending = pending.append("0");
                    et_result.setText(pending);
                    break;
                case R.id.btn_1:
                    pending = pending.append("1");
                    et_result.setText(pending);
                    break;
                case R.id.btn_2:
                    pending = pending.append("2");
                    et_result.setText(pending);
                    break;
                case R.id.btn_3:
                    pending = pending.append("3");
                    et_result.setText(pending);
                    break;
                case R.id.btn_4:
                    pending = pending.append("4");
                    et_result.setText(pending);
                    break;
                case R.id.btn_5:
                    pending = pending.append("5");
                    et_result.setText(pending);
                    break;
                case R.id.btn_6:
                    pending = pending.append("6");
                    et_result.setText(pending);
                    break;
                case R.id.btn_7:
                    pending = pending.append("7");
                    et_result.setText(pending);
                    break;
                case R.id.btn_8:
                    pending = pending.append("8");
                    et_result.setText(pending);
                    break;
                case R.id.btn_9:
                    pending = pending.append("9");
                    et_result.setText(pending);
                    break;
                case R.id.btn_jia:
                    //if (last >= '0' && last <= '9' ) {
                    pending = pending.append("+");
                    // }
                    et_result.setText(pending);
                    break;
                case R.id.btn_jian:
                    //if (last >= '0' && last <= '9') {
                    pending = pending.append("-");
                    //  }
                    et_result.setText(pending);
                    break;
                case R.id.btn_chen:
                    // if (last >= '0' && last <= '9' ) {
                    pending = pending.append("*");
                    // }
                    et_result.setText(pending);
                    break;
                case R.id.btn_chu:
                    // if (last >= '0' && last <= '9' ) {
                    pending = pending.append("/");
                    // }
                    et_result.setText(pending);
                    break;
                case R.id.btn_dian:
                    if (judje1()) {
                        pending = pending.append(".");
                        et_result.setText(pending);
                    }
                    break;

                case R.id.btn_del: //删除
                    if (pending.length() != 0) {
                        pending = pending.delete(pending.length() - 1, pending.length());
                        et_result.setText(pending);
                    }
                    break;
                case R.id.btn_clear: //清空
                    pending = pending.delete(0, pending.length());
                    et_result.setText(pending);
                    break;
                case R.id.btn_dy: // =等于
                    if ((pending.length() > 1)) {
                        InfixInToDuffix inf=new InfixInToDuffix();
                        String jieguo;
                        try {
                            String a = inf.toSuffix(pending);
                            jieguo = inf.dealEquation(a);

                        } catch (Exception ex) {
                            jieguo = "出错";
                        }
                        et_result.setText(pending + "=" + jieguo);
                        pending = pending.delete(0, pending.length());
                        if (Character.isDigit(jieguo.charAt(0))) {
                            pending = pending.append(jieguo);
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    private boolean judje1() {
        String a = "+-*/.";
        int[] b = new int[a.length()];
        int max;
        for (int i = 0; i < a.length(); i++) {
            String c = "" + a.charAt(i);
            b[i] = pending.lastIndexOf(c);
        }
        Arrays.sort(b);
        if (b[a.length() - 1] == -1) {
            max = 0;
        } else {
            max = b[a.length() - 1];
        }
        if (pending.indexOf(".", max) == -1) {
            return true;
        } else {
            return false;
        }
    }




    }

